package com.pachong.spider;

import com.common.core.WyyMusicConstant;
import com.common.core.utils.WyyMusicUtils;
import com.domain.bean.music.Song;
import com.pachong.service.MusicService;
import com.pachong.service.SongService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author : wangyu
 * Date: 2018-03-08
 * Time: 17:34
 */
@Component
public class WyySongProcessor implements PageProcessor {

    private static Logger LOGGER= LoggerFactory.getLogger(WyySongProcessor.class);
    @Autowired
    private SongService songService;
    @Resource
    private MusicService musicService;

    @Override
    public void process(Page page) {
        //判断当前爬取的是否是歌曲
        if(page.getUrl().regex(WyyMusicConstant.SONG_URL).match()){
            String s = page.getUrl().toString();
            String[] split = s.split("id=");
            this.handleSong(page, split[1].trim());
        }
    }

    private void handleSong(Page page, String code) {
        Song song = songService.findByCode(code);
        song.setUrl(page.getUrl().toString());
        if(song == null){
            return;
        }
        Html songHtml = page.getHtml();
        //选择标签DIV class类型为tit中的a中的text文本信息
        String title = songHtml.xpath("//div[@class=\"tit\"]/em/text()").toString();
        if(StringUtils.isNotBlank(title)){
            song.setName(title);
        }
        //歌曲概要信息
        Selectable songInfo = songHtml.xpath("//div[@class=\"cnt\"]");
        List<Selectable> nodes = songInfo.xpath("//p[@class=\"des s-fc4\"]").nodes();
        for (Selectable node:nodes) {
            //处理歌手信息
            if(node.toString().contains("歌手")){
                List<Selectable> singerSelects = node.xpath("//a").nodes();
                for (Selectable selectable:singerSelects){
                    List<String> all = selectable.links().all();
                    for(String url:all){
                        song.setSingerCode(song.getSingerCode()+","+url.split("id=")[1]);
                    }
                    String singer = node.xpath("//a/text()").toString();//歌手
                    song.setSinger(song.getSinger()+","+singer);
                }
            }

            //所属专辑
            if(node.toString().contains("所属专辑")){
                String album = node.xpath("//a/text()").toString();//专辑
                song.setAlbum(album);
                List<String> all = node.links().all();
                for(String url:all){
                        song.setAlbumCode(url.split("id=")[1]);
                }
            }
        }

        String wordsSong = songHtml.xpath("//div[@id=\"lyric-content\"]").toString();
        song.setWordsSong(wordsSong);

        song.setTimes(song.getTimes()+1);//爬虫次数
//        //调用处理歌曲评论信息
//        int cmtTotal = musicService.handleAllComment(code, 0, WyyMusicConstant.SONG);
//        song.setCmtTotal(cmtTotal);
        songService.saveAndFlush(song);
    }


    @Override
    public Site getSite() {
        return WyyMusicUtils.createSite();
    }

    //爬虫歌单
    public void start() {
        long start = System.currentTimeMillis();
        HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
        List<Song> musicUsers = songService.selectSong(0);
        Spider.create(this)
//                .addUrl("http://music.163.com/song?id=514761281")
                .startUrls(musicUsers.stream().map(user -> {
                    String url = "http://music.163.com/song?id="+user.getCode();
                    return url;
                }).collect(Collectors.toList()))
                .setDownloader(WyyMusicUtils.getDownloader())
                .thread(WyyMusicConstant.THREAD_NUMS)
                .run();
        long end = System.currentTimeMillis();
        System.out.println("爬虫结束,耗时--->" + WyyMusicUtils.parseMillisecone(end - start));

    }
}
